<template>
  <view class="bgColor" style="padding-bottom:calc(var(--window-bottom) + 50px);">
<!--    <uni-nav-bar style="margin-top: 80rpx;" height="100rpx"  left-icon="left" @clickLeft="clickLeft" @clickRight="clickRight" right-icon="cart" title="我的收获地址" >-->
<!--    </uni-nav-bar>-->
    <view style="height: 20rpx"></view>
    <view style="width: 90%;margin: auto;background-color: white;border-radius: 20rpx;min-height: 65rpx">
      <text style="font-weight: 600;margin: 15rpx;    line-height: 60rpx;">地址信息</text>
      <view style="margin-right: 20rpx;float: right;display: block" @click="clickRight">
        <u-image  src="/static/images/buyer/adset.png" width="30px" height="30px"></u-image>
      </view>
    </view>
    <view style="width: 100%;display: flex" v-for="(item,index) in addresses" :key="index">
      <view style="flex: 15">
        <uni-card @click="selectAddress(item)" :title="item.recipient" :extra="item.phone">
          <text class="uni-body">{{ item.detailedAddress }}</text>
          <view style="width: 100%">
            <radio :checked="item.defaultAddress" color="#f62442" />
            <u-button style="float: right" size="mini" shape="circle" @click="setDefaultAddress(item)">设为默认</u-button>
          </view>
        </uni-card>

      </view>

      <view style="flex: 1;padding-top: 80rpx;padding-right: 10rpx"  v-if="deleteShow">
<!--        <button type="primary" size="mini" style="color: white" text="删除" @click="editAddress(item)">编辑</button>-->

        <uni-icons type="compose" size="25"  @click="editAddress(item)"></uni-icons>
        <uni-icons type="trash" size="25" color="rgba(255,0,0,0.68)" @click="deleteAddress(item.eid)"></uni-icons>
<!--        <button size="mini" style="background-color: rgba(255,0,0,0.68);color: white;margin-top: 10rpx" text="删除" @click="deleteAddress(item.eid)">删除</button>-->
      </view>

    </view>
    <view style="height: 120rpx;width: 100%">

    </view>
    <view class="fix-bottom" style="width: 100%;background-color: white;display: flex;padding: 20rpx">
      <view style="flex: 1">
        <button style="background-color: #ff4d00;color: white" shape="circle" text="添加收获地址" @click="addAddress">添加收获地址</button>
      </view>
    </view>
  </view>
</template>

<script>
import {mainService} from "../../../api/buyer/main";

export default {
  name: "Address",
  data() {
    return {
      user:{},
      addresses:[],
      deleteShow:false,
    }
  },
  created(){
    this.user = uni.getStorageSync('userInfo')
    if (!this.user){
      //未登录
      uni.showToast({
        title: '未登录，请去登录',
        icon: 'none'
      });
      //成功后延迟1秒跳转
      setTimeout(() => {
        uni.navigateTo({
          url:"/pages/public/login/login",
          success:res =>{},
          fail:() =>{},
          complete:() => {}
        });
      }, 1000);
    }else{
      this.init();
    }
  },
  onShow() {
    let that = this
    uni.$on('updateAddressList',function(){
      that.init();
    })
  },
  // onUnload(){
  //   console.log("hah")
  // },
  methods:{
    init(){
      console.log(this.user)
      mainService.findShippingAddressByUserId(this.user.eid).then((res)=>{
        this.addresses = res.data
        // this.selectedAddress=res.data.length!=0?this.addresses[0]:{};
        console.log(res.data)
      })
    },
    clickLeft(){
      uni.navigateBack({
        delta: 1,
      })
    },
    addAddress(){
      uni.navigateTo({
        url: '/pages/buyer/public/AddressEdit',
      })
    },
    clickRight(){
      this.deleteShow=!this.deleteShow
    },
    deleteAddress(id){

    },
    selectAddress(item){
      uni.$emit('updateSelectAddress',item)
      this.clickLeft()
    },
    editAddress(item){
      uni.navigateTo({
        url: '/pages/buyer/public/AddressEdit?address=' + encodeURIComponent(JSON.stringify(item)),
      })
    },
    setDefaultAddress(item){
      this.addresses.forEach(address=>{
        if(address.eid==item.eid){
          address.defaultAddress=true
        }else{
          address.defaultAddress=false
        }
      })
      mainService.setDefaultAddress(item).then(res => {
        console.log("修改默认地址成功")
      }).catch(err =>{
        console.log("修改默认地址失败")
      })
    },

  }


}
</script>

<style scoped>

</style>